<?php
class Cts_Log_Error extends Cts_Log
{
	protected $logger ;
	public $aErrorLog ;
	
	public function __construct($db)
	{
	    $this->logger = new Zend_Log();
	    
		// writer
		$writer = new Zend_Log_Writer_Db( $db , 'cts_error_log' , $this->aErrorLog );
		$this->setErrorLog('0','','','') ;
		$this->aErrorLog = array(
            'error_source'  => 'uri'         , //
            'error_type'    => 'func'        , //db operation -> default
            'object_name'   => 'class'       , //
            'error_message' => 'message'     ,
            'error_trace'   => 'errortrace',
            'operator'      => 'operator'    ,
            'pid'           => 'pid'         ,
            'create_on'     => 'timestamp' 
        );
        
		$writer->setColumnMap( $this->aErrorLog ) ;
		$this->logger->addWriter($writer);
		
		// filter
		$config = Zend_Registry::get('config') ;
		$filter = new Zend_Log_Filter_Priority(intval($config->log->level));
		$this->logger->addFilter($filter);
	}
	
	public function setErrorLog( $nLoginId , $strClassName , $strFuncName , $strErrorTrace )
	{
		$this->setClass( $strClassName ) ;
		$this->setFunc( $strFuncName ) ;
		$this->setOperator( $nLoginId ) ;
		$this->setUri() ;
		$this->setErrorMessage( $strErrorTrace ) ;
		$this->setPid() ;
	}
	
	private function setOperator( $nLoginId )
	{
		$this->logger->setEventItem( 'operator', $nLoginId ) ;
	}
	
	private function setErrorMessage( $strErrorTrace )
	{
	    $config = Zend_Registry::get('config') ;
	    $bDebugLog = $config->log->debugLocal ;
	    
	    if( isset( $bDebugLog ) && intval( $bDebugLog ) == 1 )
	    {
	        $this->logger->setEventItem( 'errortrace', $strErrorTrace ) ;
	    }
	    else
	    {
	        $this->logger->setEventItem( 'errortrace', base64_encode( $strErrorTrace ) ) ;
	    }		
	}
	
	private function setUri( )
	{
	    $this->logger->setEventItem( 'uri' , $_SERVER['REQUEST_URI'] ) ;
	}
	
	private function setPid( )
	{
	    $this->logger->setEventItem( 'pid' , getmypid() ) ;
	}
}